<?php
/**
 * @copyright 2011
 * @version $Id$
 * @access public
 * @author Oriotiori

 */

Class Default_Model_Index  extends Zend_Db_Table_Abstract{

	protected $_name = 'products';

	/**
	 * obtiene banners de la seccion
	 * @param mixed $To: objeto con los datos del recurso.
	 * @return array $data
	 */
	function readBannerSeccion($To=null)
	{
		$this->_name= TABLE_PREFIX.'products';
		$TO = new Default_Model_TO_Index();

		try{

			$sql=$this->getAdapter()->select()->from(
			array('b' => $this->_name),
			array(
                                    'b.Id', 'b.Position', 'b.Description', 'b.Id_Seccion',
                                    'b.Id_Image', 'b.Seccion'
			)
			)
			->joinLeft(
			array('i' => TABLE_PREFIX.'images'),
                                'i.Id=b.Id_Image',
			array(
                                    'Medium' =>'i.Medium',
                                    'Real' =>'i.Real',
                                    'Thumb' =>'i.Thumb',
                                    'MiniThumb' =>'i.Mini_Thumb'
			)
			)
			->where('b.Seccion=?', $To['Seccion'])
			->order('b.Position ASC');

			$data0 = $this->getAdapter()->fetchAll($sql);
			$rowCount = count($data0);

			if ($rowCount > 0){
				$links = new Default_Model_Atencionaclientes();

				foreach($data0 as $dt){
					$TO->setId($dt['Id']);
					$TO->setPosition($dt['Position']);
					$TO->setMiniThumb($dt['MiniThumb']);
					$TO->setThumb($dt['Thumb']);
					$TO->setImage($dt['Medium']);
					$TO->setReal($dt['Real']);
					$TO->setIdSeccion($dt['Id_Seccion']);
					$TO->setDescription(nl2br(htmlspecialchars($dt['Description'])));
					$TO->setLink($links->createReviseLink($dt['Id_Seccion']));
					$TO->add();
				}
				$data = $TO->getAllData();
				return $data;
			}

		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}

		return null;
	}


	/**
	 * getLogoEmpresa($To)
	 * obtiene logo de la empresa
	 * @param mixed $To: objeto con los datos del recurso.
	 * @return array $data
	 */
	function getLogoEmpresa()
	{

		$TO = new Default_Model_TO_Index();

		try{

			$sql=$this->getAdapter()->select()->from(
			array('l' => TABLE_PREFIX.'logo_empresa', '*')
			)
			->joinLeft(
			array('i' => TABLE_PREFIX.'images'),
                                'i.Id=l.Id_Image',
			array('Logo' =>'i.Medium', 'i.Real', 'i.Thumb')

			)
			->limit(1)
			->order('l.Id DESC');

			$data0 = $this->getAdapter()->fetchAll($sql);
			$rowCount = count($data0);

			if ($rowCount > 0){
				foreach($data0 as $dt){
					$TO->setLogo($dt['Logo']);
					$TO->setReal($dt['Real']);
					$TO->setThumb($dt['Thumb']);
					$TO->setId($dt['Id']);
					$TO->setIdImage($dt['Id_Image']);
				}
				$data = $TO->getAllVars();
				return $data;
			}

			return null;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
	}


	/**
	 * getMessageWelcome()
	 * obtiene el mensaje de bienvenida
	 * @return array $data
	 */
	function getSeccionContent($to=null)
	{

		$TO = new Default_Model_TO_Index();

		try{

			$sql=$this->getAdapter()->select()->from(
			array('s' => TABLE_PREFIX.'secciones_content'),
			array('s.Id','s.Description', 's.Title', 's.Seccion'))
			->where('s.Seccion=?', $to['Seccion'])
			->limit(1)
			->order('s.Id DESC');

			$data0 = $this->getAdapter()->fetchAll($sql);
			$rowCount = count($data0);

			if ($rowCount > 0){
				foreach($data0 as $dt){
					$TO->setDescription($dt['Description']);
					$TO->setId($dt['Id']);
					$TO->setSeccion($dt['Seccion']);
					$TO->setTitle($dt['Title']);
				}

				$data = $TO->getAllVars();
				return $data;
			}
			return null;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}

	}


	/**
	 * getTripticos()
	 * obtiene los tripticos del post
	 * @return array $data
	 */
	function getTripticos($to=null)
	{

		$TO = new Default_Model_TO_Index();

		try{

			$sql=$this->getAdapter()->select()->from(
					array('t' => TABLE_PREFIX.'tripticos'),
					array('t.Id', 't.Name', 't.Status')
			)
			->joinLeft(
						array('f' => TABLE_PREFIX.'files'),
                        'f.Id=t.Id_File',
						array('f.Extension', 'f.File')
			)
			->where('t.Id_Zone=?', $to['Zone'])
			->order('t.Id ASC');

			if(!isset($to['Status'])){
				$sql=$sql->where('t.Status=?', 1);
			}

			$data0 = $this->getAdapter()->fetchAll($sql);
			$rowCount = count($data0);

			if ($rowCount > 0){
				foreach($data0 as $dt){
					$TO->setFile($dt['File']);
					$TO->setExtension($dt['Extension']);
					$TO->setName($dt['Name']);
					$TO->setStatus($dt['Status']);
					$TO->setId($dt['Id']);
					$TO->add();
				}

				$data = $TO->getAllData();
				return $data;
			}
			return null;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}

	}




	/**
	 * get New products
	 * @param array $to
	 * @return array $data
	 */
	public function getNewProducts($to=array())
	{
		$TO = new Admin_Model_TO_Products();
		$facadeCategories= new Admin_Model_Facade_Categories();
		$facadeProducts= new Admin_Model_Facade_Products();

		$data=array();
		try{
			$sql=$this->getAdapter()
			->select()
			->from(
				array('p' => TABLE_PREFIX.$this->_name),
				array('p.Id', 'p.Name', 'p.Id_Category', 'p.Status', 'p.Description')
			)
			->joinLeft(	array('pa' => TABLE_PREFIX.'products_attributes'),
						'p.Id=pa.Product_Id',
						array('pa.Product_Price', 'pa.Product_Model', 'pa.Product_Quantity')
			)
			->joinLeft(	array('m' => TABLE_PREFIX.'manufacturers'),
						'm.Id=p.Id_Manufacturer',
						array('m.Manufacturer')
			)
			->order('p.Id DESC')
			->limit($to['Limit'])
			->where('p.Status=?', 1);


			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);

			if($rowCount>0){
				foreach($res as $dt){
					$TO->setId($dt['Id']);
					$TO->setType('Product');
					$TO->setName($dt['Name']);
					$TO->setManufacturer($dt['Manufacturer']);
					$TO->setIdCategory($dt['Id_Category']);
					$TO->setStatus($dt['Status']);
					$TO->setPrice($dt['Product_Price']);
					$TO->setDescription($dt['Description']);
					$TO->setModel($dt['Product_Model']);
					$TO->setCantidad($dt['Product_Quantity']);
					$TO->setAllImagesByProduct($facadeProducts->getAllImagesByProduct($dt['Id']));
					$TO->setTreeCategory($facadeCategories->getTreeCatById($dt['Id_Category']));
					$TO->add();
				}
				$data = $TO->getAllData();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}


		
	/**
	 * get Featured products
	 * @param array $to
	 * @return array $data
	 */
	public function getFeaturedProducts($to=array())
	{
		$TO = new Admin_Model_TO_Products();
		$facadeCategories= new Admin_Model_Facade_Categories();
		$facadeProducts= new Admin_Model_Facade_Products();
			
		$data=array();
		try{
			$sql=$this->getAdapter()
			->select()
			->from(
				array('pf' => TABLE_PREFIX.'products_featured'),
				array( 'pf.Start_Date', 'pf.End_Date')
			)
			->joinLeft(	array('p' => TABLE_PREFIX.$this->_name),
						'p.Id=pf.Product_Id',
						array('p.Id', 'p.Name', 'p.Id_Category', 'p.Status', 'p.Description')
			)
			->joinLeft(	array('pa' => TABLE_PREFIX.'products_attributes'),
						'p.Id=pa.Product_Id',
						array('pa.Product_Price', 'pa.Product_Model', 'pa.Product_Quantity')
			)
			->joinLeft(	array('m' => TABLE_PREFIX.'manufacturers'),
						'm.Id=p.Id_Manufacturer',
						array('m.Manufacturer')
			)
			->order('p.Id DESC')
			->limit($to['Limit'])
			->where(new Zend_Db_Expr('CURDATE() between pf.Start_Date and pf.End_Date '))
			->where('p.Status=?',1);

			
			$res = $this->getAdapter()->fetchAll($sql);
			$rowCount=count($res);
				
			if($rowCount>0){
				foreach($res as $dt){
					$TO->setId($dt['Id']);
					$TO->setType('Product');
					$TO->setName($dt['Name']);
					$TO->setManufacturer($dt['Manufacturer']);
					$TO->setIdCategory($dt['Id_Category']);
					$TO->setStatus($dt['Status']);
					$TO->setPrice($dt['Product_Price']);
					$TO->setDescription($dt['Description']);
					$TO->setModel($dt['Product_Model']);
					$TO->setCantidad($dt['Product_Quantity']);
					$TO->setAllImagesByProduct($facadeProducts->getAllImagesByProduct($dt['Id']));
					$TO->setTreeCategory($facadeCategories->getTreeCatById($dt['Id_Category']));
					$TO->add();
				}
				$data = $TO->getAllData();
				return $data;
			}
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		return false;
	}
		
		

}


?>